//
//  StatusPicture.swift
//  上海微博
//
//  Created by mac on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class StatusPicture: UIView {
    
    //配图视图数组
    var pic_url : [StatusPictures]? {
        didSet{
            //隐藏所有图像
            for v in subviews {
                v.hidden=true
            }
            //遍历数组 一次设置图像
            var index = 0
            
            for url in (pic_url ?? []) {
                let v = subviews[index++] as! UIImageView
                //设置图像
                v.cz_setImageWithURL(url.thumbnail_pic, placeholderName: nil)
                //显示图像
                v.hidden=false
                //设置图像的填充样式
                v.contentMode = .ScaleAspectFill
                //处理四张图片
                if index==2 && pic_url?.count==4 {
                    index++
                }
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
       
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
///MARK: 设置界面
extension StatusPicture {
    
    private func setupUI(){
        clipsToBounds=true
        backgroundColor=superview?.backgroundColor
            //每张图像的bouns
             let rect = CGRect(origin: CGPointZero, size: cz_statusLayout.pictureSize)
            //移动步长
            let step =  cz_statusLayout.pictureSize.width+cz_statusLayout.HpictureMargin
        //添加9个图像视图
        for i in 0..<9 {
            
            let imageView = UIImageView()
            
            //imageView.backgroundColor=UIColor.redColor()
            //根据i 计算视图所在的位置
            //所在行/列
            let row = CGFloat( i/Int(cz_statusLayout.pictureRowCount))
            let col = CGFloat( i%Int(cz_statusLayout.pictureRowCount))
            //设置位置
            imageView.frame=CGRectOffset(rect, step*col, step*row)
            imageView.clipsToBounds=true
            addSubview(imageView)
        }
        
    }
}